package pilgun.Multithread.CircularBuffer;


class Producer implements Runnable{
	private CircularBuffer cb;
	private boolean closed = false;
	private static String[] data = {"1", "2", "3", "4", "5", "6", "7", "8" , "9", "DONE"};
	
	public Producer (CircularBuffer cb){
		this.cb = cb;
	}
	
	public void run(){
		try {
			for (int i = 0; i < data.length; i++){
				cb.put(data[i]);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class Consumer implements Runnable{
	private CircularBuffer cb;
	
	public Consumer (CircularBuffer cb){
		this.cb = cb;
	}
	
	public void run(){
		try {
			for (String message = cb.get(); !"DONE".equals(message) ; message = cb.get()){
				System.out.printf("MESSAGE RECEIVED: %s%n", message);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

public class CircularBufferTry{
	private static int BUFFER_LENGTH = 5;
	public static void main(String[] args) {
		CircularBuffer cb = new CircularBufferImpl2(BUFFER_LENGTH);
//		CircularBuffer cb = new CircularBufferImpl(BUFFER_LENGTH);
		Thread producerThread = new Thread(new Producer(cb));
		Thread consumerThread = new Thread(new Consumer(cb));
		producerThread.start();
		consumerThread.start();
	}
	
}